﻿namespace Poet
{
    /// <summary>
    /// Interaction logic for GoToLineDialog.xaml
    /// </summary>
    public partial class GoToLineDialog
    {
        private int _lineNumber = 1;
        private readonly int _minLineNumber = 1;
        private readonly int _maxLineNumber = 1;
        public int LineNumber { get { return _lineNumber; } }

        public GoToLineDialog()
        {
            InitializeComponent();

            _rangeNumber.Content =
                _rangeNumber.Content +
                string.Format(" ({0}-{1}):", _minLineNumber, _maxLineNumber);

            _lineNumberTextBox.Text = _lineNumber.ToString();
            _lineNumberTextBox.Focus();
            _lineNumberTextBox.SelectAll();
        }

        public GoToLineDialog(int minLineNumber, int maxLineNumber, int currentLineNumber = 0)
        {
            InitializeComponent();

            if (minLineNumber < 1) minLineNumber = 1;
            if (maxLineNumber < minLineNumber) maxLineNumber = minLineNumber;
            if (currentLineNumber < minLineNumber) currentLineNumber = minLineNumber;
            if (currentLineNumber > maxLineNumber) currentLineNumber = maxLineNumber;

            _minLineNumber = minLineNumber;
            _maxLineNumber = maxLineNumber;
            _lineNumber = currentLineNumber;

            _rangeNumber.Content =
                _rangeNumber.Content +
                string.Format(" ({0}-{1}):", _minLineNumber, _maxLineNumber);

            _lineNumberTextBox.Text = _lineNumber.ToString();
            _lineNumberTextBox.Focus();
            _lineNumberTextBox.SelectAll();
        }

        private void LineNumberTextBoxTextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            _okButton.IsEnabled =
                int.TryParse(_lineNumberTextBox.Text, out _lineNumber) &&
                _minLineNumber <= _lineNumber && _lineNumber <= _maxLineNumber;
        }

        private void OkButtonClick(object sender, System.Windows.RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }
    }
}
